package cn.shop.merchant.controller.player;

import cn.dev33.satoken.stp.StpUtil;
import cn.shop.merchant.req.PlayerQueryReq;
import cn.shop.merchant.req.StatusReq;
import cn.shop.merchant.service.PlayerAccountService;
import cn.shop.utils.ApiResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

import static cn.shop.utils.constant.Constant.UID;

/**
 * 玩家管理接口
 * @author : 李一涵
 * @date : Created in 2021-11-3 13:58
 * @description : 玩家账号信息controller
 */
@RestController
@RequestMapping("/player/account")
public class PlayerAccountController {

    @Resource
    private PlayerAccountService playerAccountService;

    /**
     * 获取玩家信息列表
     *
     * @param req 查询条件
     * @return 返回结果
     */
    @GetMapping("/list")
    public ApiResult<Map<String, Object>> list(PlayerQueryReq req) throws Exception {
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("用户编号为空，请检查登录状态");
        }
        Map<String, Object> data = playerAccountService.list(req, uid);
        return ApiResult.success(data);
    }

    /**
     * 修改账号状态
     *
     * @param ids 编号
     * @param status 状态
     * @return 返回结果
     */
    @PutMapping("/change/status/{ids}")
    public ApiResult<Map<String, Object>> changeStatus(@PathVariable("ids") int[] ids, @RequestBody StatusReq status) {
        if (ids == null || ids.length == 0) {
            return ApiResult.fail("参数错误");
        }
        long uid = StpUtil.getSession().getLong(UID);
        if (uid == 0) {
            return ApiResult.fail("用户编号为空，请检查登录状态");
        }
        boolean isSuccess = playerAccountService.changeStatus(ids, uid, status.getStatus());
        return ApiResult.result(isSuccess);
    }

    /**
     * 强制解绑
     *
     * @param id 编号
     * @return 返回结果
     */
    @DeleteMapping("/unbind/{id}")
    public ApiResult<Map<String, Object>> unbind(@PathVariable long id) {
        long uid = StpUtil.getSession().getLong(UID);
        return ApiResult.result(playerAccountService.unbind(uid, id));
    }
}
